package com.sgb.member.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.sgb.member.BR;
import com.sgb.member.R;
import com.sgb.member.databinding.MitemInviteMemberBinding;
import com.sgb.member.entity.QualityMemberBean;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class MemberInviteAdapter extends BaseRecycleAdapter<QualityMemberBean> {

    private final List<QualityMemberBean> mSelectList = new ArrayList<>();
    private final Map<String, Boolean> mSelectMap = new HashMap<>();

    private final boolean isSignChoose;

    public MemberInviteAdapter(Context context, List<QualityMemberBean> datas, boolean isSignChoose) {
        super(context, R.layout.mitem_invite_member, datas);
        mSelectList.clear();
        mSelectMap.clear();
        this.isSignChoose = isSignChoose;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        String companyLogo = datas.get(position).getCompanyLogo();
        ImageView quality_member_logo = holder.itemView.findViewById(R.id.quality_member_logo);
        Glide.with(context).load(companyLogo).placeholder(R.drawable.member_icon_company_logo).error(R.drawable.member_icon_company_logo).into(quality_member_logo);
    }

    @Override
    public void convert(ItemViewHolder holder, final QualityMemberBean entity) {
        holder.setBinding(BR.viewModel, entity);
        MitemInviteMemberBinding binding = (MitemInviteMemberBinding) holder.getmBinding();
        if (entity.isChecked()) {
            binding.checkLayout.setOnClickListener(null);
            binding.checkBox.setBackgroundResource(R.mipmap.member_checked_gray);
        } else {
            Boolean isCheck = mSelectMap.get(entity.getNo());
            if (isCheck == null) {
                isCheck = false;
            }
            if (isCheck) {
                binding.checkBox.setBackgroundResource(R.mipmap.member_icon_xuanzhong);
            } else {
                binding.checkBox.setBackgroundResource(R.mipmap.member_icon_weigouxuan2);
            }
            binding.checkLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Boolean aBoolean = mSelectMap.get(entity.getNo());
                    boolean isChecked = aBoolean != null && aBoolean;
                    if (isSignChoose) {
                        mSelectList.clear();
                        mSelectMap.clear();
                        mSelectMap.put(entity.getNo(), !isChecked);
                    } else {
                        if (mSelectList.size() >= 5 && !isChecked) {
                            MToast.showToast(view.getContext(), "您一次最多可以勾选5个企业");
                            return;
                        }
                    }
                    mSelectMap.put(entity.getNo(), !isChecked);
                    if (isChecked) {
                        mSelectList.remove(entity);
                    } else {
                        mSelectList.add(entity);
                    }
                    notifyDataSetChanged();
                    onCheckedUpdate(mSelectList);
                }
            });
        }
    }

    public List<QualityMemberBean> getSelectList() {
        return mSelectList;
    }

    public abstract void onCheckedUpdate(List<QualityMemberBean> array);

    @Override
    public void setDatas(List<QualityMemberBean> list) {
        mSelectList.clear();
        mSelectMap.clear();
        super.setDatas(list);
    }

}
